<?php
include("../kernel/db.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>LD-CMS 3 by LD-Network.de</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="setup.css" />
</head>
<body>
<div id="header">
<h1>Installationsroutine</h1><h3>f&uuml;r das LD-CMS 3</h3>
</div>
<div id="navigationLeiste">
 <div id="sectionLinks">
<a href="http://board.ld-network.de" class="menu">Support-Forum</a>
</div>
</div>
<div id="inhalt">
<?php
if(!isset($_GET['do']) && $_GET['do'] != "install") {
	// Check CHMOD
?>
	<div id="outertable">

	<form action="" name="checkChmod" method="get">
	<table>
	<tr>
	    <td colspan="2" class="tablehead">Alle unten angegebenen Ordner m&uuml;ssen die CHMOD-Rechte 777 haben<br>
	    W&auml;hrend der Installation muss ./conf/ die Rechte 777 gegeben werden!</td>
	</tr>
	<tr>
	    <td style="text-align:center;">./content/</td>
	    <td style="text-align:center;">
			<?php
				if (is_writeable("../content/")) echo "<font color=\"green\">Richtig</font>";
	            else echo "<font color=\"red\">Falsch</font>";
			?>
		</td>
	</tr>
	<tr>
	    <td style="text-align:center;">./conf/</td>
	    <td style="text-align:center;">
			<?php
				if (is_writeable("../conf/")) echo "<font color=\"green\">Richtig</font>";
	            else echo "<font color=\"red\">Falsch</font>";
			?>
		</td>
	</tr>
	<tr>
	    <td style="text-align:center;">./modules</td>
	    <td style="text-align:center;">
			<?php
				if (is_writeable("../modules/")) echo "<font color=\"green\">Richtig</font>";
	            else echo "<font color=\"red\">Falsch</font>";
			?>
		</td>
	</tr>
	<tr>
	    <td style="text-align:center;">./smilies/</td>
	    <td style="text-align:center;">
			<?php
				if (is_writeable("../smilies/")) echo "<font color=\"green\">Richtig</font>";
	            else echo "<font color=\"red\">Falsch</font>";
			?>
		</td>
	</tr>
	<tr>
	    <td style="text-align:center;">./templates/standard/templatesCache</td>
	    <td style="text-align:center;">
			<?php
				if (is_writeable("../templates/standard/templatesCache/")) echo "<font color=\"green\">Richtig</font>";
	            else echo "<font color=\"red\">Falsch</font>";
			?>
		</td>
	</tr>
	<tr>
	    <td style="text-align:center;">.htaccess</td>
	    <td style="text-align:center;">
			<?php
				if (is_writeable("../.htaccess")) echo "<font color=\"green\">Richtig</font>";
	            else echo "<font color=\"red\">Falsch</font>";
			?>
		</td>
	</tr>
	</table>
	<br /><br />
	<div align="right">
		<input type="submit" name="s" value="Weiter" />
	</div>
	<input type="hidden" name="do" value="install" />
	</form>

	</div>

<?php
}else{
	// Check for errors?
	$errors = "";
	$highlightFields = array();
	if(isset($_POST['install'])) {
		if($_POST['urltocms'] == "") {
			$errors .= "<li>Sie m&uuml;ssen die Url zum CMS angeben</li>";
			$highlightFields[] = "urltocms";
		}
		/*if($_POST['absolute_path'] == "") {
			$errors .= "<li>Sie m&uuml;ssen den absoluten Pfad zum CMS angeben</li>";
			$highlightFields[] = "absolute_path";
		}*/
		if($_POST['db_host'] == "") {
			$errors .= "<li>Sie m&uuml;ssen einen Datenbank-Host angeben</li>";
			$highlightFields[] = "db_host";
		}
		if($_POST['db_user'] == "") {
			$errors .= "<li>Sie m&uuml;ssen einen Datenbank-User angeben</li>";
			$highlightFields[] = "db_user";
		}
		if($_POST['db_password'] == "") {
			$errors .= "<li>Sie m&uuml;ssen einen Datenbank-Passwort angeben</li>";
			$highlightFields[] = "db_password";
		}
		if($_POST['db_name'] == "") {
			$errors .= "<li>Sie m&uuml;ssen einen Datenbank-Name angeben</li>";
			$highlightFields[] = "db_name";
		}	
			
		if($_POST['title'] == "") {
			$errors .= "<li>Sie m&uuml;ssen einen Titel angeben</li>";
			$highlightFields[] = "title";
		}
		if($_POST['slogan'] == "") {
			$errors .= "<li>Sie m&uuml;ssen einen Slogan angeben</li>";
			$highlightFields[] = "slogan";
		}
		
		
		if($_POST['username'] == "") {
			$errors .= "<li>Sie m&uuml;ssen einen Usernamen angeben</li>";
			$highlightFields[] = "username";
		}
		if($_POST['password'] == "") {
			$errors .= "<li>Sie m&uuml;ssen ein Passwort angeben</li>";
			$highlightFields[] = "password";
		}
		if($_POST['re_password'] == "") {
			$errors .= "<li>Sie m&uuml;ssen das Passwort wiederholen</li>";
			$highlightFields[] = "re_password";
		}
		
		if($_POST['password'] != $_POST['re_password']) {
			$errors .= "<li>Die Passw&ouml;rter stimmen nicht &uuml;berein</li>";
			$highlightFields[] = "password";
			$highlightFields[] = "re_password";
		}
		
		
		if($_POST['email'] == "") {
			$errors .= "<li>Sie m&uuml;ssen eine Email angeben</li>";
			$highlightFields[] = "email";
		}
		
		
		
		
		
		if($errors == "") {
			// Check DB-Connection
			$handler = @mysql_connect($_POST['db_host'],$_POST['db_user'],$_POST['db_password']);
     		if (!@mysql_select_db($_POST['db_name'],$handler)) {
				$errors .= "<li>Ihre MySQL-Daten sind falsch. Bitte kontrollieren Sie ihre Eingaben</li>";
			}
		}	
	}
	
	if(!isset($_POST['install']) || $errors != "") {
		// Absoluter Pfad
        $abpath = ereg_replace ("\\\\","/",__FILE__);
        $abpath = trim(dirname ($abpath)).'/';
		$formvalue['absolute_path'] = substr($abpath,0,strpos($abpath,'setup'));
        $formvalue['urltocms'] = 'http://'.$_SERVER['SERVER_NAME'].substr($_SERVER['REQUEST_URI'],0,strpos($_SERVER['REQUEST_URI'],'setup'));
		
		foreach($_POST as $key=>$value) {
			$formvalue[$key] = $value;
		}
		
		if(isset($errors) && $errors != "") {
			echo "<h1>Fehler</h1><ul>".$errors."</ul>";
		}
		?>
		<form name="install" action="" method="post">
		<div id="outertable">
		
		<table>
		<tr>
	    	<td colspan="2" class="tablehead"><h3>Globale Einstellungen</h3></td>
	    </tr>
	    <tr>
	    	<td style="width:20%; vertical-align:top;">URL zum CMS:</td>
	    	<td <?php if(in_array("urltocms",$highlightFields)) echo "style='background-color:red;'"; ?>>
				<input type="text" name="urltocms" value="<?php echo $formvalue['urltocms']; ?>" size="30" />
				<div style="font-size: smaller; margin-bottom: 2ex;">Die URL zum CMS (http://...) mit abschlie&szlig;endem "/".</div>
			</td>
	    </tr>
	<!--
		<tr>
	    	<td style="width:20%; vertical-align:top;">Absoluter Pfad:</td>
	    	<td <?php if(in_array("absolute_path",$highlightFields)) echo "style='background-color:red;'"; ?>>
				<input type="text" name="absolute_path" value="<?php echo $formvalue['absolute_path']; ?>" size="30" />
				<div style="font-size: smaller; margin-bottom: 2ex;">Der Absolute Pfad zum CMS</div>
			</td>
	    </tr>
	-->
	    <tr>
	    	<td class="tablehead" colspan="2"><h3>Mysql Daten</h3></td>
	    </tr>
	    <tr>
	    	<td style="width:20%; vertical-align:top;">Mysql Host:</td>
	    	<td <?php if(in_array("db_host",$highlightFields)) echo "style='background-color:red;'"; ?>>
				<input type="text" name="db_host" value="<?php echo $formvalue['db_host']; ?>" size="30" />
				<div style="font-size: smaller; margin-bottom: 2ex;">Meistens localhost. Fragen Sie notfalls bei Ihrem Hoster nach.</div>
			</td>
	    </tr>
	    <tr>
	    	<td style="width:20%; vertical-align:top;">Mysql Username:</td>
	    	<td <?php if(in_array("db_user",$highlightFields)) echo "style='background-color:red;'"; ?>>
				<input type="text" name="db_user"value="<?php echo $formvalue['db_user']; ?>" size="30" />
				<div style="font-size: smaller; margin-bottom: 2ex;">Der Username zum verbinden mit der Datenbank.</div>
			</td>
	    </tr>
	    <tr>
	    	<td style="width:20%; vertical-align:top;">Mysql Passwort:</td>
	    	<td <?php if(in_array("db_password",$highlightFields)) echo "style='background-color:red;'"; ?>>
				<input type="password" name="db_password" size="30" />
				<div style="font-size: smaller; margin-bottom: 2ex;">Das zum Usernamen passende Passwort wird hier ben&ouml;tigt.</div>
			</td>
	    </tr>
	    <tr>
	    	<td style="width:20%; vertical-align:top;">Mysql Datenbank:</td>
	    	<td <?php if(in_array("db_name",$highlightFields)) echo "style='background-color:red;'"; ?>>
				<input type="text" name="db_name" value="<?php echo $formvalue['db_name']; ?>" size="30" />
				<div style="font-size: smaller; margin-bottom: 2ex;">Geben Sie hier die Datenbank an, in die Sie das Forum installieren wollen.</div>
			</td>
	    </tr>
	    <tr>
	    	<td style="width:20%; vertical-align:top;">Mysql Pr&auml;fix:</td>
	        <td>
				<input type="text" name="db_pre" value="<?php echo $formvalue['db_pre']; ?>" size="30" />
				<div style="font-size: smaller; margin-bottom: 2ex;">Wenn Sie mehrere CMS in einer Datenbank laufen lassen wollen, geben Sie bitte jedem eine andere Nummer, da sich sonst die Datenbanken &uuml;berschreiben.</div>
			</td>
	    </tr>
		<tr>
          <td colspan="2" class="tablehead"><h3>Globale Einstellungen</h3></td>
        </tr>
        <tr>
        	<td style="width:20%; vertical-align:top;">Titel</td>
        	<td <?php if(in_array("title",$highlightFields)) echo "style='background-color:red;'"; ?>>
				<input type="text" name="title" value="<?php echo $formvalue['title']; ?>" size="30" />
				<div style="font-size: smaller; margin-bottom: 2ex;">Der Titel der Seite.</div>
			</td>
        </tr>
		<tr>
        	<td style="width:20%; vertical-align:top;">Slogan</td>
        	<td <?php if(in_array("slogan",$highlightFields)) echo "style='background-color:red;'"; ?>>
				<input type="text" name="slogan" value="<?php echo $formvalue['slogan']; ?>" size="30" />
				<div style="font-size: smaller; margin-bottom: 2ex;">Der Slogan der Seite</div>
			</td>
        </tr>
		<tr>
        	<td style="width:20%; vertical-align:top;">Mod_rewrite benutzen?</td>
        	<td>
				<select name="useModRewrite">
					<option value="0">Ja</option>
					<option value="1" <?php if($formvalue['useModRewrite'] == "1") echo'selected="selected"'; ?>>Nein</option>
				</select>
				<div style="font-size: smaller; margin-bottom: 2ex;">Soll mod_rewrite f&uuml;r Suchmaschinenfreundliche URL's verwendet werden?</div>
			</td>
        </tr>
		<tr>
        	<td colspan="2" class="tablehead"><h3>Adminuser</h3></td>
        </tr>
        <tr>
        	<td style="width:20%; vertical-align:top;">Username</td>
        	<td <?php if(in_array("username",$highlightFields)) echo "style='background-color:red;'"; ?>>
				<input type="text" name="username" value="<?php echo $formvalue['username']; ?>" size="30" />
				<div style="font-size: smaller; margin-bottom: 2ex;">Der Username des Admins</div>
			</td>
        </tr>
		<tr>
        	<td style="width:20%; vertical-align:top;">Passwort</td>
        	<td <?php if(in_array("password",$highlightFields)) echo "style='background-color:red;'"; ?>>
				<input type="password" name="password" size="30" />
				<div style="font-size: smaller; margin-bottom: 2ex;">Das Passwort des Admins</div>
			</td>
        </tr>
		<tr>
        	<td style="width:20%; vertical-align:top;">Passwort wiederholen</td>
        	<td <?php if(in_array("re_password",$highlightFields)) echo "style='background-color:red;'"; ?>>
				<input type="password" name="re_password" size="30" />
				<div style="font-size: smaller; margin-bottom: 2ex;">Bitte wiederholen sie das Passwort.</div>
			</td>
        </tr>
		<tr>
        	<td style="width:20%; vertical-align:top;">Email</td>
        	<td <?php if(in_array("email",$highlightFields)) echo "style='background-color:red;'"; ?>>	
				<input type="text" name="email" value="<?php echo $formvalue['email']; ?>" size="30" />
				<div style="font-size: smaller; margin-bottom: 2ex;">Die Email-Adresse des Admins</div>
			</td>
        </tr>
		<tr>
	    	<td colspan="2"><div align="right"><input type="submit" name="install" value="Installieren" /></div></td>
	    </tr>
	    </table>
		
		</div>
		</form>
		<?php
	}else{
		
		echo "Creating config.php...";
		// delete the config.data.php
        if (file_exists("../conf/config.php")) unlink("../conf/config.php");           
        
        // Now, lets create it.
        $fp = fopen("../conf/config.php", "w+");
        fwrite($fp, "<?php\n");
        fwrite($fp, "$"."sql['host'] = \"". $_POST['db_host']."\";\n");
        fwrite($fp, "$"."sql['db'] = \"". $_POST['db_name']."\";\n");
        fwrite($fp, "$"."sql['user'] = \"". $_POST['db_user']."\";\n");
        fwrite($fp, "$"."sql['password'] = \"". $_POST['db_password']."\";\n");
        fwrite($fp, "$"."sql['pre'] = \"". $_POST['db_pre']."_\";\n\n");
        
        fwrite($fp, "$"."config_file['urlZumCMS'] = \"".$_POST['urltocms']."\";\n");
        fwrite($fp, '?>');
        fclose($fp);
		echo "<font style='color:green;'>Erfolgreich</font><br />";
		
		// Create Tables
		echo "Creating Tables...";
		$pre = $_POST['db_pre'];
		
		@set_time_limit(300);
        include("installQueries.php");
        
		DB::getInstance()->initDatabase($_POST['db_host'], $_POST['db_name'],$_POST['db_user'], $_POST['db_password']);

        foreach ($queries as $key=>$value)  {
            DB::getInstance()->query($value,false);
        }
		echo "<font style='color:green;'>Erfolgreich</font><br />";
		
		echo "Bitte kopieren Sie die conf/config.php in das Root-Verzeichnis vom CMS";
	}
	
}
?>

</div>

</body>
</html>
